package com.crm.system.service.impl.csd.customerManager;

import com.alibaba.fastjson.JSONArray;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.csd.WorkLogEnum;
import com.crm.model.entity.csd.customerManager.CustomerServiceWorkLog;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.csd.customerManager.CustomerContactPersonVO;
import com.crm.model.vo.csd.customerManager.CustomerServiceWorkLogVO;
import com.crm.service.csd.customerManager.CustomerContactPersonListDbService;
import com.crm.system.service.csd.customerManager.CustomerContactPersonListService;
import com.crm.system.service.csd.customerManager.CustomerServiceWorkLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wangpeng
 * @Date 2021/8/26 15:07
 */
@Service
public class CustomerContactPersonListServiceImpl implements CustomerContactPersonListService  {

    @Autowired
    private CustomerContactPersonListDbService customerContactPersonListDbService;

    @Autowired
    private CustomerServiceWorkLogService workLogService;

    /**
     * 客户列表--查看--联系人管理列表
     * @param customerId --客户ID
     * @param pageIndex  --页码
     * @param pageSize   --分页大小
     * @param sortName   --排序字段
     * @param sortOrder  --排序规则
     * @author wangpeng
     * @return
     */
    @Override
    public DhPageInfo<CustomerContactPersonVO> selectCustomerContactPersonList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String customerId,String auditStatus, String onTheJobSituation) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }

        }
        List<Integer> auditStatuss = JSONArray.parseArray(auditStatus,Integer.class);
        List<Integer> onTheJobSituations = JSONArray.parseArray(onTheJobSituation,Integer.class);
        List<CustomerContactPersonVO> CustomerContactPersonList =customerContactPersonListDbService.selectCustomerContactPersonList(customerId,order.toString(),auditStatuss,onTheJobSituations);
        PageInfo<CustomerContactPersonVO> pageInfo = new PageInfo<CustomerContactPersonVO>(CustomerContactPersonList);
        return new DhPageInfo<CustomerContactPersonVO>(pageInfo, CustomerContactPersonList);
    }


    /**
     * 客户列表--查看--联系人管理列表--审核
     * @param contactPersonId --客户ID
     * @param pageIndex  --页码
     * @param pageSize   --分页大小
     * @param sortName   --排序字段
     * @param sortOrder  --排序规则
     * @author wangpeng
     * @return
     */
    @Override
    public DhPageInfo<CustomerServiceWorkLogVO> selectAuditWorkLog(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String contactPersonId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }

        }
        List<CustomerServiceWorkLogVO> customerWorkLogList =customerContactPersonListDbService.selectAuditWorkLog(contactPersonId,order.toString());
        PageInfo<CustomerServiceWorkLogVO> pageInfo = new PageInfo<CustomerServiceWorkLogVO>(customerWorkLogList);
        return new DhPageInfo<CustomerServiceWorkLogVO>(pageInfo, customerWorkLogList);
    }

    /**
     * 客户列表--查看--联系人管理列表--审核--修改审核
     * @author wangpeng
     * @param customerId   客户ID
     * @param contactName  联系人
     * @param contactInformation  联系方式
     * @param auditStatus  审核状态
     * @param remarks      备注
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean updateCustomerContactPersonByCustomerId(String customerId,String contactPersonId,String contactName,String contactInformation,Integer auditStatus,String remarks) {
        //联系人id查看数据比较是否一致
        CustomerContactPersonVO customerContactPersonVO = customerContactPersonListDbService.selectCustomerContactPersonBycontactPersonId(contactPersonId);
        //获取当前登录人
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        String accountId = userAccount.getAccountId();
        String userName = userAccount.getUserName();
        //日志类型
        short logType = WorkLogEnum.LOG_TYPE_CUSTOMER.getCode().shortValue();
        //工作内容  登录人+操作内容
        String logContent = userName;
        String name = customerContactPersonVO.getContactName();
        if(!contactName.equals(name) && contactName != null && contactName !=""){
            logContent+=" 将联系人修改为"+contactName+" ";
        }
        String information = customerContactPersonVO.getContactInformation();
        if (!contactInformation.equals(information) && contactInformation !=null && contactInformation !=""){
            logContent+="将联系方式修改为"+contactInformation+" ";
        }
        Integer status = customerContactPersonVO.getAuditStatus();
        if(auditStatus != status && auditStatus != null){
            if (auditStatus == 0){
                logContent+="将审核状态改为正确";
            }else if(auditStatus == 1){
                logContent+="将审核状态改为错误";
            }else if(auditStatus == 2){
                logContent+="将审核状态改为未审核";
            }
        }
        //备注   备注信息+操作人
        String logremarks =   remarks+"  "+ userName;
        //新增工作日志(csd_work_log)
        CustomerServiceWorkLog customerServiceWorkLog = workLogService.getCustomerServiceWorkLog(accountId,customerId,logType,logContent,logremarks,contactPersonId);
        workLogService.insertWorkLog(customerServiceWorkLog);
        return customerContactPersonListDbService.updateCustomerContactPersonByCustomerId(contactPersonId,contactName,contactInformation,auditStatus);
    }

    /**
     * 客户列表--查看--联系人管理列表--审核--回显审核状态
     * @author wangpeng
     * @param contactPersonId
     */
    @Override
    public CustomerContactPersonVO selectCustomerContactPersonBycontactPersonId(String contactPersonId) {
        return customerContactPersonListDbService.selectCustomerContactPersonBycontactPersonId(contactPersonId);
    }
}
